import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router';
import { forkJoin, Observable } from 'rxjs';
import { first } from 'rxjs/operators';
import { MemberService } from '@services/member.service';
import { RecordVal, User, UserSheet } from '@services/data-types/member.type';

type CenterDataType = [User, RecordVal[], UserSheet];

@Injectable()
export class CenterResolveService implements Resolve<CenterDataType> {
  constructor(private memberService: MemberService, private router: Router) {}
  resolve(route: ActivatedRouteSnapshot): Observable<CenterDataType> {
    const uid = route.paramMap.get('id');
    if (uid) {
      return forkJoin(
        this.memberService.getUserDetail(uid),
        this.memberService.getUserRecord(uid),
        this.memberService.getUserSheets(uid)
      ).pipe(first());
    } else {
      this.router.navigate(['/home']).then();
    }
  }
}
